package com.hmkx.app.util.log;

import com.alibaba.fastjson.JSON;
import com.hmkx.app.util.http.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 报警、异常提醒工具类
 * @author hht
 *   2017-12-27
 */
public class WarnPushUtil {
	
	public static Logger logger = LoggerFactory.getLogger(WarnPushUtil.class);
	
	/**
	 * 发送异常通知
	 * @param errorTitle 异常简介
	 * @param errorInfo 异常详细信息
	 * @param level 异常级别  0 普通  1严重
	 */
	public static void sendWarnInfo(String errorTitle, String errorInfo, int level){
		try{
			Map ddMesMap = new HashMap();//发送到钉钉机器人的消息
			Map contentMap = new HashMap();
			contentMap.put("content", "异常简要："+errorTitle+";异常明细："+errorInfo);
			ddMesMap.put("msgtype", "text");
			ddMesMap.put("text", contentMap);
			//技术群
			HttpClientUtil.post("https://oapi.dingtalk.com/robot/send?access_token=dac1f55e372815dd0e5d346818c3ac6648c6f8f04ad14f4ce5125b90e9811efa", JSON.toJSONString(ddMesMap));
		}catch (Exception e) {
			logger.error("异常通知工具类运行异常");
		}
		
	}
	/**
	 * 发送异常通知
	 * @param errorTitle 异常简介
	 * @param errorObject 异常对象
	 * @param level 异常级别  0 普通  1严重
	 */
	public static void sendWarnInfo(String errorTitle, Exception errorObject, int level){
		try{
			StringWriter sw = new StringWriter();
			errorObject.printStackTrace(new PrintWriter(sw));
			Map ddMesMap = new HashMap();//发送到钉钉机器人的消息
			Map contentMap = new HashMap();
			contentMap.put("content", "异常简要："+errorTitle+";异常明细："+sw.toString());
			ddMesMap.put("msgtype", "text");
			ddMesMap.put("text", contentMap);
			logger.error(ddMesMap.toString());
			//技术群
//			HttpClientUtil.post("https://oapi.dingtalk.com/robot/send?access_token=dac1f55e372815dd0e5d346818c3ac6648c6f8f04ad14f4ce5125b90e9811efa", JSON.toJSONString(ddMesMap));
			
		}catch (Exception e) {
			logger.error("异常通知工具类运行异常");
		}
		
	}
	
	
}
